package com.wu.ssm.mybatis;

import java.sql.*;


/**
 * Created by wzf on 2018/11/11/011.
 */
public class SimpleExcutor implements Excutor {

    @Override
    public <T> T query(String sql, Object [] parameter) {

        Connection connection = null;
        connection = getConnection();
        try {
            PreparedStatement pre = connection.prepareStatement(sql);
            System.out.println("执行语句为：" + sql);
            pre.setLong(1,Long.valueOf(parameter[0].toString()));
            ResultSet set = pre.executeQuery();
            User user = new User();
            while (set.next()){
                user.setId(set.getLong(1));
                user.setUsername(set.getString(2));
            }
            return (T) user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private Connection getConnection() {
        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/ace-admin?characterEncoding=UTF-8";
        String username="root";
        String password="123456";
        try {
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, username, password);
            connection.setAutoCommit(true);
            return connection;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


}
